<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 定时发送订单模型
 * @package app\admin\model
 */
class BusBatchOrder extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_batch_order';

    public function getAllData($request)
    {
            $data = $this::alias('bt')
            ->field('bt.*, bc.company_name')
            ->join('bus_channel bc', 'bc.channel_id=bt.channel_id')
            ->where('bt.create_time', 'between time', [$request['st'], $request['et']]);

        //渠道通道用户只能查到自己的订单
        switch (session('user_type')) {

            case 'admin':

                if (!empty($request['cha'])) {

                    $data = $data->where('bt.channel_id', $request['cha']);
                }
                break;

            case 'channel':

                $data = $data->where('bt.channel_id', session('relate_id'));
                break;

            default:

                return [];
                break;
        }

        if ($request['send_status'] !== '') {

            $data = $data->where('bt.send_status', $request['send_status']);
        }

        $dataarr = $data->order('bt.create_time desc')
                ->paginate($request['limit'], false, ['query'=>$request])
                ->each(function($item, $key){

                    switch ($item->send_status) {
                        case '1':
                            $item->send_status_ = '<span class="layui-badge layui-bg-green">已发送</span>';
                            break;
                        case '0':
                            $item->send_status_ = '<span class="layui-badge layui-bg-orange event-stop">未发送</span>';
                            break;
                        case '2':
                            $item->send_status_ = '<span class="layui-badge">取消发送</span>';
                            break;

                        default:
                            $item->send_status_ = '';
                            break;
                    }

                    $item->batch_num = $item->id;


                    // if (!empty($item->phones)) {

                    //     $item->phones_ = '<a href="javascript:;" onclick="findPhone('.$item->id.')" style="color:#3498db"><u>查看</u></a>';
                    // } else {

                    //     $item->phones_ = '';
                    // }

                });
        //返回数据集
        return $dataarr;
    }
}